package com.seuic.hnjiningposapp.hnapps.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.cardview.widget.CardView;

import com.alibaba.fastjson.JSONObject;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.seuic.hnjiningposapp.R;
import com.seuic.hnjiningposapp.hnapps.base.BaseActivity;
import com.seuic.hnjiningposapp.hnapps.ui.view.dialog.AutomaticOperationPopup;
import com.seuic.hnjiningposapp.hnapps.ui.view.dialog.DealingPopup;
import com.seuic.hnjiningposapp.hnapps.ui.view.dialog.EcashSavePp;
import com.seuic.hnjiningposapp.hnapps.ui.view.dialog.ErrorPopup;
import com.seuic.hnjiningposapp.hnapps.ui.view.dialog.InputPasswordPopup;
import com.seuic.hnjiningposapp.hnapps.ui.view.dialog.InputSaveInfoPp;
import com.seuic.hnjiningposapp.hnapps.ui.view.dialog.PrintingPopup;
import com.seuic.hnjiningposapp.hnapps.ui.view.dialog.QuerybalancePp;
import com.seuic.hnjiningposapp.hnapps.ui.view.dialog.ReadCardPopup;
import com.seuic.hnjiningposapp.hnapps.ui.view.dialog.SureDealPopup;
import com.seuic.hnjiningposapp.hnapps.ui.view.dialog.SurePrintPopup;
import com.seuic.hnjiningposapp.hnapps.ui.view.dialog.VerifyCardPopup;
import com.seuic.hnjiningposapp.hnapps.util.GetMoneyLvUtils;
import com.seuic.hnjiningposapp.hnapps.util.HttpUtils;
import com.seuic.hnjiningposapp.hnapps.util.HttpUtilsV2;
import com.seuic.hnjiningposapp.hnapps.util.UiTool;
import com.socks.library.KLog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 电子签约
 */
public class EcashActivity extends BaseActivity implements ErrorPopup.OnFinishCallbacked, QuerybalancePp.OnFinishCallBack, SurePrintPopup.ClickCallback, EcashSavePp.OnSureEcSaveCallBack, SureDealPopup.OnNextClicklistener, InputSaveInfoPp.SureInputSaveCallBack, InputPasswordPopup.ClickCallback {

    //电子现金
    private static String MAKE_TYPE = "";
    //电子现金圈存
    private static String SAVE = "SAVE";
    //电子现金查询
    private static String QUERY = "QUERY";
    @BindView(R.id.base_back_btn)
    LinearLayout baseBackBtn;
    @BindView(R.id.base_title_text)
    TextView baseTitleText;
    @BindView(R.id.re_select_ones_btn)
    Button reSelectOnesBtn;
    @BindView(R.id.re_select_others_btn)
    Button reSelectOthersBtn;
    @BindView(R.id.local_layout)
    CardView localLayout;
    private BasePopupView inputpasswordPopupView;
    private InputPasswordPopup inputpasswordPop;
    private BasePopupView verifycardPopupView;
    private VerifyCardPopup verifycardPop;
    private BasePopupView readcardPopupView;
    private ReadCardPopup readcardPop;
    private BaseActivity baseActivity;
    private String card_num;
    private AutomaticOperationPopup automaticOperationPopup;
    private InputSaveInfoPp inputSaveInfoPp;

    private BasePopupView inputSaveInfoPpView;
    private SureDealPopup sureDealPopup;
    private BasePopupView sureDealPopupView;
    private DealingPopup dealingPopup;
    private BasePopupView dealingPopupView;
    private EcashSavePp ecashSavePp;
    private BasePopupView ecashSavePpView;
    private SurePrintPopup surePrintPop;
    private BasePopupView surePrintPopupView;
    private PrintingPopup printingPop;
    private BasePopupView printingPopupView;
    private QuerybalancePp querybalancePp;
    private BasePopupView querybalancePpView;
    private ErrorPopup errorPopup;
    private BasePopupView errorPopupPpView;
    //加密密码
    private String cipassword;
    //电子现金余额
    private String crecAcctBalance;
    private String card_username;

    @Override
    public int initView(Bundle savedInstanceState) {
        return R.layout.activity_ecash;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        setTitleText("电子现金");
        findViewById(R.id.base_back_btn).setVisibility(View.INVISIBLE);
        errorPopup = new ErrorPopup(this, this);
        errorPopupPpView = new XPopup.Builder(this)
                .dismissOnBackPressed(false)
                .dismissOnTouchOutside(false)
                .moveUpToKeyboard(false)
                .isRequestFocus(false)
                .hasShadowBg(false)
                .hasStatusBarShadow(false)
                .asCustom(errorPopup);
        querybalancePp = new QuerybalancePp(this, this);
        querybalancePpView = new XPopup.Builder(this)
                .dismissOnBackPressed(false)
                .dismissOnTouchOutside(false)
                .moveUpToKeyboard(false)
                .isRequestFocus(false)
                .hasShadowBg(false)
                .hasStatusBarShadow(false)
                .asCustom(querybalancePp);
        //打印纸是否正常界面
        surePrintPop = new SurePrintPopup(this, this, this);
        surePrintPopupView = new XPopup.Builder(this)
                .dismissOnBackPressed(false)
                .dismissOnTouchOutside(false)
                .moveUpToKeyboard(false)
                .isRequestFocus(false)
                .hasShadowBg(false)
                .hasStatusBarShadow(false)
                .asCustom(surePrintPop);
        printingPop = new PrintingPopup(this, this);
        //打印中界面
        printingPopupView = new XPopup.Builder(this)
                .dismissOnBackPressed(false)
                .dismissOnTouchOutside(false)
                .moveUpToKeyboard(false)
                .isRequestFocus(false)
                .hasShadowBg(false)
                .hasStatusBarShadow(false)
                .asCustom(printingPop);
        ecashSavePp = new EcashSavePp(this, this);
        //交易中
        ecashSavePpView = new XPopup.Builder(this)
                .dismissOnBackPressed(false)
                .dismissOnTouchOutside(false)
                .moveUpToKeyboard(false)
                .isRequestFocus(false)
                .hasShadowBg(false)
                .hasStatusBarShadow(false)
                .asCustom(ecashSavePp);
        dealingPopup = new DealingPopup(this, this);
        dealingPopupView = new XPopup.Builder(this)
                .dismissOnBackPressed(false)
                .dismissOnTouchOutside(false)
                .moveUpToKeyboard(false)
                .isRequestFocus(false)
                .hasShadowBg(false)
                .hasStatusBarShadow(false)
                .asCustom(dealingPopup);//交易中
        sureDealPopup = new SureDealPopup(this, this);
        //确认输入圈存信息
        sureDealPopupView = new XPopup.Builder(this)
                .dismissOnBackPressed(false)
                .dismissOnTouchOutside(false)
                .moveUpToKeyboard(false)
                .isRequestFocus(false)
                .hasShadowBg(false)
                .hasStatusBarShadow(false)
                .asCustom(sureDealPopup);
        inputSaveInfoPp = new InputSaveInfoPp(this, this);
        inputSaveInfoPpView = new XPopup.Builder(this)
                .dismissOnBackPressed(false)
                .dismissOnTouchOutside(false)
                .moveUpToKeyboard(false)
                .isRequestFocus(false)
                .hasShadowBg(false)
                .hasStatusBarShadow(false)
                .asCustom(inputSaveInfoPp);//输入圈存信息
        readcardPop = new ReadCardPopup(this);
        //验证账户信息界面
        readcardPopupView = new XPopup.Builder(this)
                .dismissOnBackPressed(false)
                .dismissOnTouchOutside(false)
                .moveUpToKeyboard(false)
                .isRequestFocus(false)
                .hasShadowBg(false)
                .hasStatusBarShadow(false)
                .asCustom(readcardPop);
        verifycardPop = new VerifyCardPopup(this, this);
        //验证账户信息界面
        verifycardPopupView = new XPopup.Builder(this)
                .dismissOnBackPressed(false)
                .dismissOnTouchOutside(false)
                .moveUpToKeyboard(false)
                .isRequestFocus(false)
                .hasShadowBg(false)
                .hasStatusBarShadow(false)
                .asCustom(verifycardPop);
        inputpasswordPop = new InputPasswordPopup(this,  this);
        //输入密码界面
        inputpasswordPopupView = new XPopup.Builder(this)
                .dismissOnBackPressed(false)
                .dismissOnTouchOutside(false)
                .moveUpToKeyboard(false)
                .isRequestFocus(false)
                .hasShadowBg(false)
                .hasStatusBarShadow(false)
                .asCustom(inputpasswordPop);
    }

    @Override
    public void initEvent(Bundle savedInstanceState) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.re_select_ones_btn, R.id.re_select_others_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.re_select_ones_btn:
                //电子现金圈存
                MAKE_TYPE = SAVE;
                break;
            case R.id.re_select_others_btn:
                //电子现金查询
                MAKE_TYPE = QUERY;
                break;
        }
        localLayout.setVisibility(View.GONE);

        readcardstart();//读卡操作
    }

    /**
     * 读卡
     */
    private void readcardstart() {
        readCard(new HttpUtils.BaseReadCardListener() {
            @Override
            public void onSuccess(String data, String track2Data) {
                card_num = data;
                verifycardPopupView.show();
                HttpUtilsV2.getCheckingThisBanks(data, new HttpUtilsV2.NetCallBack() {
                    @Override
                    public void onSuccess(String ServerSeqNo, String ConsumerSeqNo, JSONObject rspBody) {
                        crecAcctBalance = rspBody.getString("CRECAcctBalance");

                        runOnUiThread(() -> {
                            verifycardPopupView.dismiss();
                            KLog.e("进行验证本行卡数据,验证通过,进行输入密码");
                            if (!inputpasswordPopupView.isShow()) {
                                inputpasswordPopupView.show();
                            }
                        });
                    }

                    @Override
                    public void onError(String code, String msg) {
                        showErr(msg);
                    }
                });
            }

            @Override
            public void onError(String msg) {
                showErr(msg);
            }
        });
    }

    private void showErr(String msg) {
        runOnUiThread(() -> {
            errorPopup.setMsg(msg);
            errorPopupPpView.show();
        });
    }

    /**
     * 输入密码的回调
     *
     * @param password
     */
    @Override
    public void SendPassword(String password) {
        KLog.e(password + "card_num");
        cipassword = HttpUtils.getResult(password, card_num);
        inputpasswordPopupView.dismiss();
        businessOperation();
    }

    /**
     * 操作
     */
    private void businessOperation() {
        //弹出验证消息
        showMakeHint("验证密码...");
        HttpUtilsV2.getAccountPwdVerification(card_num, cipassword, new HttpUtilsV2.NetCallBack() {
            @Override
            public void onSuccess(String ServerSeqNo, String ConsumerSeqNo, JSONObject rspBody) {
                runOnUiThread(() -> {
                    KLog.e("验证密码成功,返回数据，进行获取客户信息");
                    dismissshowMakeHint();
                    showMakeHint("获取客户信息...");
                    HttpUtilsV2.getQueryBankCardInfo(card_num, new HttpUtilsV2.NetCallBack() {
                        @Override
                        public void onSuccess(String ServerSeqNo, String ConsumerSeqNo, JSONObject rspBody) {
                            card_username = rspBody.getString("AcctName");
                            runOnUiThread(() -> {
                                //客户信息获取成功
                                dismissshowMakeHint();
                                //黑名单验证
                                showMakeHint("黑名单验证...");
                                HttpUtilsV2.getCheckingBankCardBlacklist(card_num, card_username, new HttpUtilsV2.NetCallBack() {
                                    @Override
                                    public void onSuccess(String ServerSeqNo, String ConsumerSeqNo, JSONObject rspBody) {
                                        runOnUiThread(() -> {
                                            dismissshowMakeHint();
                                            //开始输入圈存信息
                                            if (MAKE_TYPE == SAVE) {
                                                inputSaveInfoPp.setmBalance(GetMoneyLvUtils.getMoney(crecAcctBalance));
                                                inputSaveInfoPp.setmCardNum(card_num);
                                                inputSaveInfoPp.setmUserName(card_username);
                                                inputSaveInfoPpView.show();
                                            } else {
                                                querybalancePp.setType("电子现金余额");
                                                querybalancePp.setCard_username(card_username);
                                                querybalancePp.setCard_num(card_num);
                                                querybalancePp.setCard_balance(GetMoneyLvUtils.getMoney(crecAcctBalance));
                                                querybalancePpView.show();
                                            }
                                        });
                                    }

                                    @Override
                                    public void onError(String code, String msg) {
                                        showErr(msg);
                                    }
                                });
                            });
                        }

                        @Override
                        public void onError(String code, String msg) {
                            showErr(msg);
                        }
                    });
                });
            }

            @Override
            public void onError(String code, String msg) {
                showErr(msg);
            }
        });
    }


    /**
     * 输入圈存信息的回调
     *
     * @param money 输入的金额
     */
    @Override
    public void SureInputSaveCallBack(String money) {
        inputSaveInfoPpView.dismiss();
        sureDealPopup = null;
        sureDealPopupView = null;


        sureDealPopup = new SureDealPopup(this, this);
        //确认输入圈存信息
        sureDealPopupView = new XPopup.Builder(this)
                .dismissOnBackPressed(false)
                .dismissOnTouchOutside(false)
                .moveUpToKeyboard(false)
                .isRequestFocus(false)
                .hasShadowBg(false)
                .hasStatusBarShadow(false)
                .asCustom(sureDealPopup);

        sureDealPopup.setmUserName(card_username);
        sureDealPopup.setmOperation_name("请确认圈存信息");
        sureDealPopup.setmMoney(GetMoneyLvUtils.getInputMoney(money));
        sureDealPopup.setmCardNum(card_num);
        sureDealPopupView.show();
    }

    /**
     * 确认圈存信息 下一步  进行交易
     */
    @Override
    public void onNext() {
        sureDealPopupView.dismiss();
        dealingPopupView.show();

//        HttpUtilsV2.getElectronicCashTransfer(
//                "0",
//                card_num,
//                ""
//
//        );
        UiTool.getElectronicCashTransferUiTool(baseActivity,
                "", "", "", "", "", "", "", "", "",
                "", "", data -> {
                    dealingPopupView.dismiss();
                    ecashSavePpView.show();
                });
    }

    /**
     * 确认圈存信息 上一步
     */
    @Override
    public void LastStep() {
        sureDealPopupView.dismiss();

        inputSaveInfoPp = null;
        inputSaveInfoPpView = null;
        inputSaveInfoPp = new InputSaveInfoPp(this, this);
        inputSaveInfoPpView = new XPopup.Builder(this)
                .dismissOnBackPressed(false)
                .dismissOnTouchOutside(false)
                .moveUpToKeyboard(false)
                .isRequestFocus(false)
                .hasShadowBg(false)
                .hasStatusBarShadow(false)
                .asCustom(inputSaveInfoPp);//输入圈存信息
        inputSaveInfoPp.setmUserName(card_username);
        inputSaveInfoPp.setmCardNum(card_num);
        inputSaveInfoPp.setmBalance(GetMoneyLvUtils.getMoney(crecAcctBalance));
        inputSaveInfoPpView.show();

    }

    /**
     * 确认交易处理成功  完成 打印
     */
    @Override
    public void OnSureEcSaveCallBack() {
        ecashSavePpView.dismiss();
        surePrintPopupView.show();
    }

    @Override
    public void ToPrint() {
        surePrintPopupView.dismiss();
        printingPopupView.show();
        if (MAKE_TYPE == SAVE) {
//        PrintUtil.printThfwmOrElectronicCash()
        } else {

        }
    }

    /**
     * 电子现金余额查询成功
     */
    @Override
    public void Onfinish() {
        finish();
    }

    @Override
    public void finished() {
        finish();
    }
}
